<?php


class Tpl_Mail_Send {

	
	public $path = 'static/emails/';
	var $_data =  array();
	var $_subject = '';
	function __construct(){
		$this->CI = & get_instance();
		$this->CI->load->library(array('parser','php_mailer'));
		
		
	    $this->CI->config->load ( 'email',true );
		$config = $this->CI->config->config ['email'];
		
		log_message ( 'debug', 'Class Send Mail Loaded');
		
		if (count ( $config ) > 0) {
			$this->initialize ( $config );
		}
		
		
		
	}
	
	
	/**
	 * Initialize the preferences
	 *
	 * Accepts an associative array as input, containing display preferences
	 *
	 * @access public
	 * @param 	array config preferences
	 * @return void
	 */
	function initialize($config = array()) {
		foreach ( $config as $key => $val ) {
			$this->$key = $val;
		}
	}
	function subject($subject){
		$this->_subject = (string) $subject;
	}
	
	function data($data){
		$this->_data = $data;
	}
	function to($email){
		$this->to = $email;
	}
	function send($id, $view = ''){
		$parser = $this->CI->parser;
		$config = $this->$id;
		//view parte
		$view = trim($this->path,'/').'/'.$view;

		$data['content'] = $this->CI->parser->parse( $view , $this->_data,TRUE);
	    $data['IMAGE'] = base_url('/public/images/logo.png');
		
	    $tpl = trim($this->path,'/').'/tpl';
		
	    $message = utf8_decode($this->CI->parser->parse($tpl,$data,true));
	    
	    $mail = $this->CI->php_mailer;
	  
	    $mail->IsSMTP(); // telling the class to use SMTP
	    $mail->Host       = $config['smtp_host']; // SMTP server
	    //$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
	    // 1 = errors and messages
	    // 2 = messages only
	    $mail->SMTPAuth   = true;                  // enable SMTP authentication
	    $mail->Port       = $config['smtp_port']; 
	    $mail->Host       = $config['smtp_host']; // sets the SMTP server                  // set the SMTP port for the GMAIL server
	    $mail->Username   = $config['smtp_user'];  // GMAIL username
	    $mail->Password   = $config['smtp_pass'];            // GMAIL password
	    $mail->SetFrom($config['from'],utf8_decode( $config['from_label']));
	    $mail->Subject    = utf8_decode( $this->_subject );

	    
	    $mail->MsgHTML($message);

        if(ENVIRONMENT == 'development'){

            $isEmailConfig = $this->CI->config->config['email'];
            if(isset($isEmailConfig['send_all'])){
                $to = $isEmailConfig['send_all']['to'];
                $mail->AddAddress($to);
                if(!$mail->Send())
                    return true;
                return false;
            }
        }
        if(is_array($this->to)){
                foreach ($this->to as $e_mail ){
                    $mail->AddAddress($e_mail);
                }
            }else{
	      $mail->AddAddress($this->to);
            }
            if(!$mail->Send()) {
	    	//$this->errors = $mail->ErrorInfo;
	    	return false;  
	    } else {
	    	return TRUE;
	    }
	}
	
}